﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public static class Notification
    {
        public delegate void NodeOperationEvent(NodeOperationEventArgs e);
        public static event NodeOperationEvent BeginOperationEvent;
        public static event NodeOperationEvent EndOperationEvent;
        public static event NodeOperationEvent FailOperationEvent;
        public static event NodeOperationEvent DebugMessageEvent;

        readonly static Stack<NodeOperationEventArgs> OperationStack = new Stack<NodeOperationEventArgs>();

        public static int Depth { get { return OperationStack.Count(); } }

        public static void BeginOperation(string message, NodeOperationEventArgs.EventImportance importance)
        {
            NodeOperationEventArgs e = new NodeOperationEventArgs()
            {
                Importance = importance,
                Message = message
            };
            //Raise the event
            BeginOperationEvent(e);

            OperationStack.Push(e);
        }

        public static void EndOperation()
        {
            NodeOperationEventArgs e = OperationStack.Pop();
            EndOperationEvent(e);
        }

        public static void DebugMessage(string message)
        {

           NodeOperationEventArgs e = new NodeOperationEventArgs
            {
                Importance = NodeOperationEventArgs.EventImportance.Debug,
                Message = message
            };

            //Raise the event
            DebugMessageEvent(e);
        }

        internal static void FailOperation(Exception exception, NodeOperationEventArgs.EventImportance errorImportance)
        {
            //Clear the operation stack; process has been aborted and there is no further use for the those items
            OperationStack.Clear();

            NodeOperationEventArgs e = new NodeOperationEventArgs
            {
                Importance = errorImportance,
                Message = exception.Message,
                OperationException = exception
            };

            //Raise the event
            FailOperationEvent(e);
        }
    }
}
